﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace lcs.Web.DemandPlan
{
    public partial class DemandPlanSubmit : System.Web.UI.Page
    {
        Maticsoft.BLL.workFlow wm = new Maticsoft.BLL.workFlow();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    int dpid = Convert.ToInt32(Request["id"]);
                    List<Maticsoft.Model.documentFlowInfo> li = new Maticsoft.BLL.documentFlowInfo().GetModelList(" document_id = " + dpid);

                    if (li.Count == 1)
                    {
                        Maticsoft.Model.documentFlowInfo d = li[0];

                        
                        if (d.status != 0)
                        {
                            this.btn_submit.Text = "已提交审核";
                            this.btn_submit.Enabled = false;
                        }

                    }
                }

                if (!string.IsNullOrEmpty(Request["type"]))
                {
                    if (Request["type"] == "check")
                    {
                        this.btn_submit.Visible = false;
                    }
                }
            }
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                int person_id = 0;
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);
                }

                int dpid = Convert.ToInt32(Request["id"]);

                Maticsoft.BLL.documentFlowInfo dfm = new Maticsoft.BLL.documentFlowInfo();
                List<Maticsoft.Model.documentFlowInfo> li = dfm.GetModelList(" document_id = " + dpid);

                if (li.Count == 1)
                {
                    Maticsoft.Model.documentFlowInfo d = li[0];

                    d.status = 1;
                    dfm.Update(d);//更改状态为审批中

                    DataSet ds = wm.GetWorkFlow(d.flow_id.Value, 1);

                    if (ds.Tables[0].Rows.Count > 0)//判断0节点处是否需要会签
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            Maticsoft.Model.workFlow w = new Maticsoft.Model.workFlow();
                            w.document_id = d.document_id;
                            w.flow_node_id = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_id"].ToString());
                            w.flow_node_num = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_num"].ToString());
                            w.person_id = Convert.ToInt32(ds.Tables[0].Rows[i]["person_id"].ToString());
                            w.workFlow_direction = 0;//代表此节点是正向，非反向（退签）
                            w.workFlow_reson = "";
                            w.workFlow_status = 0;//审批状态(0、未审批，1、同意，2、否决(退回首节点))
                            w.workFlow_time = DateTime.Now;//提交日期
                            if (Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_isAutoNext"]) == 0)
                            {
                                w.workFlow_isWork = false;
                            }
                            else
                            {
                                w.workFlow_isWork = true;
                            }
                            w.workFlow_submitId = person_id;

                            new Maticsoft.BLL.workFlow().Add(w);//添加一条新流程

                        }

                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('提交成功！请耐心等待审批结果');location.href='../../admin/demandplaninfo/List.aspx';</script>");
                    }

                }



            }


        }
    }
}